<template>
  <div>
    <nav-bar title="编辑联系人"/>
    <div class="wrap">
      <van-address-edit
        :address-info="addressInfo"
        :area-list="areaList"
        show-postal
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="deleteContact"
      />
    </div>
  </div>
</template>

<script>
import NavBar from '../../components/NavBar.vue'
import { areaList } from '@vant/area-data'
import { contactUpdate, fetchContactInfo, deleteContact } from '@api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      areaList,
      // 跳转到修改联系人的时候，保留联系人的初始值
      addressInfo: {}
    }
  },
  created () {
    this.fetchContactInfo()
  },
  methods: {
    deleteContact () {
      deleteContact({
        id: this.$route.query.id,
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.code === 0) {
          Toast({
            type: 'success',
            message: '删除成功',
            onClose: () => {
              this.$router.push('/contactLists')
            }
          })
        }
      })
    },
    fetchContactInfo () {
      // 获取编辑页联系人的初始值
      fetchContactInfo({
        id: this.$route.query.id,
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.code === 0) {
          const obj = {}
          const contact = res.data.data.info
          for (const key in contact) {
            switch (key) {
              case 'id':
                obj.id = contact[key]
                break
              case 'linkMan':
                obj.name = contact[key]
                break
              case 'mobile':
                obj.tel = contact[key]
                break
              case 'address':
                obj.address = contact[key]
                break
              case 'isDefault':
                obj.isDefault = contact[key]
                break
              case 'cityStr':
                obj.county = contact[key]
                break
              default:
                break
            }
          }
          this.addressInfo = obj
        }
      })
    },
    onSave (contact) {
      // 点击保存按钮
      console.log(contact)
      const obj = {}
      for (const key in contact) {
        switch (key) {
          case 'addressDetail':
            obj.address = contact[key]
            break
          case 'areaCode':
            obj.cityId = contact[key]
            break
          case 'name':
            obj.linkMan = contact[key]
            break
          case 'tel':
            obj.mobile = contact[key]
            break
          case 'postalCode':
            obj.provinceId = contact[key]
            break
          default:
            break
        }
      }
      obj.isDefault = contact.isDefault
      obj.id = this.$route.query.id
      console.log(obj)
      contactUpdate({
        ...obj,
        token: localStorage.getItem('token')
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          Toast({
            type: 'success',
            message: '添加成功',
            onClose: () => {
              this.$router.push('/contactLists')
            }
          })
        }
      })
    }
  },
  components: {
    NavBar
  }

}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 46px;
}
</style>
